package com.zzyl.nursing.controller.test;

import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.service.AlertWebSocketService;
import com.zzyl.nursing.websocket.AlertMessage;
import com.zzyl.nursing.websocket.AlertWebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * WebSocket测试控制器
 * 
 * @author zzyl
 * @date 2025-07-25
 */
@Api(tags = "WebSocket测试接口")
@RestController
@RequestMapping("/test/websocket")
@Slf4j
public class WebSocketTestController {

    @Autowired
    private AlertWebSocketService alertWebSocketService;

    @ApiOperation("发送测试报警消息给指定用户")
    @PostMapping("/sendAlert/{userId}")
    public AjaxResult sendTestAlert(
            @ApiParam("用户ID") @PathVariable String userId,
            @ApiParam("设备ID") @RequestParam(defaultValue = "687a560932771f177b4d67f3_watch_0001") String iotId,
            @ApiParam("功能名称") @RequestParam(defaultValue = "HeartRate") String functionName,
            @ApiParam("数据值") @RequestParam(defaultValue = "45") String dataValue) {
        
        try {
            // 创建测试设备数据
            DeviceData testDeviceData = DeviceData.builder()
                    .iotId(iotId)
                    .deviceName("测试设备")
                    .productName("智能监护设备")
                    .functionId(functionName)
                    .dataValue(dataValue)
                    .physicalLocationType(2) // 床位
                    .deviceDescription("F001,R101,B001") // 楼层,房间,床位
                    .accessLocation("1号楼101房间1号床")
                    .alarmTime(LocalDateTime.now())
                    .build();

            // 发送报警
            alertWebSocketService.sendDeviceAlert(testDeviceData, 3); // 高等级报警
            
            log.info("成功发送测试报警给用户: {}", userId);
            return AjaxResult.success("测试报警发送成功");
            
        } catch (Exception e) {
            log.error("发送测试报警失败", e);
            return AjaxResult.error("发送测试报警失败: " + e.getMessage());
        }
    }

    @ApiOperation("广播测试报警消息给所有用户")
    @PostMapping("/broadcast")
    public AjaxResult broadcastAlert(
            @ApiParam("报警消息") @RequestParam(defaultValue = "紧急报警测试") String message) {
        
        try {
            AlertMessage alertMessage = AlertMessage.builder()
                    .notifyType(1) // 报警异常
                    .isAllConsumer(true) // 广播
                    .physicalLocationType(0) // 楼层
                    .deviceDescription("F001,,") // 1号楼异常
                    .accessLocation("1号楼")
                    .productName("楼层监控系统")
                    .functionName("FireAlarm")
                    .dataValue("检测到烟雾")
                    .alertLevel(4) // 紧急
                    .createTime(LocalDateTime.now())
                    .build();

            AlertWebSocketServer.broadcastAlert(alertMessage);
            
            log.info("成功广播测试报警消息");
            return AjaxResult.success("广播测试报警成功");
            
        } catch (Exception e) {
            log.error("广播测试报警失败", e);
            return AjaxResult.error("广播测试报警失败: " + e.getMessage());
        }
    }

    @ApiOperation("发送解除报警消息")
    @PostMapping("/resolve")
    public AjaxResult resolveAlert(
            @ApiParam("设备描述") @RequestParam(defaultValue = "F001,R101,B001") String deviceDescription) {
        
        try {
            alertWebSocketService.sendResolveAlert(deviceDescription, 2);
            
            log.info("成功发送解除报警消息");
            return AjaxResult.success("解除报警消息发送成功");
            
        } catch (Exception e) {
            log.error("发送解除报警消息失败", e);
            return AjaxResult.error("发送解除报警消息失败: " + e.getMessage());
        }
    }

    @ApiOperation("获取在线用户信息")
    @GetMapping("/online")
    public AjaxResult getOnlineInfo() {
        try {
            int onlineCount = alertWebSocketService.getOnlineUserCount();
            return AjaxResult.success()
                    .put("onlineCount", onlineCount)
                    .put("onlineUsers", AlertWebSocketServer.getOnlineUsers());
                    
        } catch (Exception e) {
            log.error("获取在线用户信息失败", e);
            return AjaxResult.error("获取在线用户信息失败: " + e.getMessage());
        }
    }

    @ApiOperation("检查用户是否在线")
    @GetMapping("/isOnline/{userId}")
    public AjaxResult checkUserOnline(@ApiParam("用户ID") @PathVariable String userId) {
        try {
            boolean isOnline = alertWebSocketService.isUserOnline(userId);
            return AjaxResult.success()
                    .put("userId", userId)
                    .put("isOnline", isOnline);
                    
        } catch (Exception e) {
            log.error("检查用户在线状态失败", e);
            return AjaxResult.error("检查用户在线状态失败: " + e.getMessage());
        }
    }
} 